package com.sbpm.crmbpm.client.util;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.sbpm.crmbpm.client.grid.CompanyGrid;
import com.sbpm.crmbpm.client.panels.CompanySearchPanel;
import com.sbpm.crmbpm.client.services.CompanyManagement;
import com.sbpm.crmbpm.client.services.CompanyManagementAsync;
import com.sbpm.crmbpm.shared.data.client.Company;

import java.util.List;

public class CompanySearchUtils
{
    private final CompanyManagementAsync companyManagement = GWT.create( CompanyManagement.class );

    private CompanySearchPanel searchPanel;
    private CompanyGrid companyGrid;

    public CompanySearchUtils( CompanySearchPanel searchPanel, CompanyGrid companyGrid )
    {
        this.searchPanel = searchPanel;
        this.companyGrid = companyGrid;
    }

    public void search()
    {
        companyManagement.findCompanies( searchPanel.getClientName(), searchPanel.getClientName(),
                searchPanel.getInn(), searchPanel.getOgrn(), searchPanel.getKpp(),
                searchPanel.getSegment(), new SimpleAsyncCallback< List< Company > >()
                {
                    @Override
                    public void onSuccess( List< Company > result )
                    {
                        companyGrid.getStore().clear();
                        companyGrid.getStore().addAll( result );
                        companyGrid.getView().refresh( false );
                    }
                } );
    }

}
